package com.zsl.casuallyheart.reg;

import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.update.BmobUpdateAgent;

import com.zsl.casuallyheart.AppBaseActivity;
import com.zsl.casuallyheart.R;
import com.zsl.casuallyheart.bean.User;
import com.zsl.casuallyheart.login.Login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 注册
 * 
 * @author zsl
 * @date 2014-10-5下午4:07:33
 * @version v1.0
 */
public class Register extends AppBaseActivity {
	/**
	 * 控件
	 */

	private EditText reg_et_username;
	private EditText reg_et_pwd;
	private EditText reg_et_repwd;
	private TextView reg_bt_reg;
	private TextView reg_bt_login;
	/**
	 * 数据
	 */
	String username, pwd, repwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initView();

	}

	/**
	 * 初始化控件
	 */
	private void initView() {
		reg_et_username = (EditText) findViewById(R.id.register_et_username);
		reg_et_pwd = (EditText) findViewById(R.id.register_et_pwd);
		reg_et_repwd = (EditText) findViewById(R.id.register_et_repwd);
		reg_bt_reg = (TextView) findViewById(R.id.register_bt_reg);
		//注册
		reg_bt_reg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				username = reg_et_username.getText().toString();
				pwd = reg_et_pwd.getText().toString();
				repwd = reg_et_repwd.getText().toString();

				if (TextUtils.isEmpty(username)) {
					Toast.makeText(Register.this, "用户名不能为空", Toast.LENGTH_SHORT)
							.show();
				} else if (TextUtils.isEmpty(pwd)) {
					Toast.makeText(Register.this, "密码不能为空", Toast.LENGTH_SHORT)
							.show();
				} else if (!pwd.equals(repwd)) {
					Toast.makeText(Register.this, "两次密码不一致", Toast.LENGTH_SHORT)
							.show();
				} else {
					reg(Register.this);
				}
			}
		});

		reg_bt_login = (TextView) findViewById(R.id.register_bt_login);
		reg_bt_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		
	}
	/**
	 * 注册
	 */
	private void reg(final Context context) {
		User user = new User();
		user.setUsername(username);
		user.setPassword(pwd);
		user.signUp(Register.this, new SaveListener() {
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				Toast.makeText(context, "注册成功,请登录", Toast.LENGTH_LONG)
						.show();
				onBackPressed();
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "注册失败，请重试"+arg1, Toast.LENGTH_SHORT)
						.show();
			}
		});
	}
}
